package com.woniu.mapper;

import com.woniu.been.entity.Teachers;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeachersMapper {
    /*
    * 登录
    * */
    @Select("SELECT * FROM teachers WHERE teacher_name = #{name} AND password = #{pwd}")
    Teachers login(@Param("name") String teacherName, @Param("pwd") String password);

    /*
    * 动态查询老师信息进行分页
    * */
    List<Teachers> findAllTeacher(Teachers teachers);
    /*
     * 根据姓名和手机号查询老师（目的:判断该用户名或者手机号是否存在 如果存在则不允许注册）
     * */
    @Select("SELECT * FROM teachers WHERE teacher_name = #{teacherName} or telephone = #{telephone}")
    List<Teachers> findTeacherByNameAndTelephone(String teacherName, String telephone);
    /*
     * 注册老师信息
     * */
    @Insert("INSERT INTO teachers(teacher_name,password,telephone,role,img_path,code,create_name,create_time,amend_name,amend_time) " +
            "VALUES(#{teacherName},#{password},#{telephone},2,#{imgPath},#{code},#{teacherName},now(),#{amendName},#{amendTime})")
    void register(Teachers teachers);

    /*
     * 删除老师信息
     * */
    @Delete("DELETE FROM teachers WHERE teacher_id = #{teacherId}")
    void deleteTeacher(Integer teacherId);

    /*
     * 修改老师信息
     * */
    void updateTeacher(Teachers teachers);

    /*
     * 根据用户名查询老师角色
     * */
    @Select("SELECT tr.t_role_name FROM teachers t JOIN t_role tr ON t.role=tr.t_role_id WHERE teacher_name = #{teacherName}")
    String findTeacherRoleByTeacherName(String teacherName);

    /*
     * 根据手机号查询老师角色
     * */
    @Select("SELECT tr.t_role_name FROM teachers t JOIN t_role tr ON t.role=tr.t_role_id WHERE telephone = #{telephone}")
    String findTeacherRoleByTelephone(String telephone);

    /*
     * 添加管理员
     * */
    @Insert("INSERT INTO teachers(teacher_name,password,telephone,role,img_path,code,create_name,create_time,amend_name,amend_time) " +
            "VALUES(#{teacherName},#{password},#{telephone},1,#{imgPath},#{code},#{teacherName},now(),#{amendName},#{amendTime})")
    void addAdmin(Teachers teachers);

    /*
     * 根据用户名查询用户
     * */
    @Select("SELECT * FROM teachers WHERE teacher_name = #{teacherName}")
    Teachers findTeacherByName(String teacherName);

    /*
     * 查询除了自己以外的老师
     * */
    @Select("SELECT * FROM teachers WHERE (teacher_name = #{teacherName} OR telephone = #{telephone}) AND teacher_id != #{teacherId}")
    List<Teachers> findTeacherNotContainItself(String teacherName, String telephone, Integer teacherId);

    /*
     * 根据手机号查询用户名
     * */
    @Select("SELECT teacher_name FROM teachers WHERE telephone = #{telephone}")
    String findTeacherNameByTelephone(String telephone);

    /*
     * 修改用户头像
     * */
    @Update("UPDATE teachers SET img_path = #{param1} WHERE teacher_name = #{param2}")
    void updateByUsername(String imagePath, String teacherName);

    /*
     * 根据用户id查询用户角色
     * */
    @Select("SELECT role FROM teachers WHERE teacher_id = #{teacherId}")
    Integer findRoleByTeacherId(Integer teacherId);

    /*
     * 根据用户id查询用户
     * */
    @Select("SELECT * FROM teachers WHERE teacher_id = #{teacherId}")
    Teachers findTeacherById(Integer teacherId);

    /*
     * 修改用户角色
     * */
    @Update("UPDATE teachers SET role = #{role} WHERE teacher_name = #{teacherName}")
    void updateTeacherRole(String teacherName, Integer role);
}
